﻿
@{
    ViewBag.Title = "淘宝订单-已审核";
    Layout = "~/Views/Shared/_LayoutEasyUI.cshtml";
}

<table id="grid-list"> </table>

<div id="toolbar" style="display:none;">
    <div style="margin: 10px 0 5px 20px;  height: 35px;">
        <a id="btnSync" href="#" class="easyui-linkbutton btn" data-options="iconCls:'icon-reload'">同步订单</a>
        <a id="btnAllot" href="#" class="easyui-linkbutton btn" data-options="iconCls:'icon-man'">分配物流</a>
    </div>
</div>

@section scripts{
    <script>
        var supplierGrid = {
            GetChecked: function () {
            }
        };
        $(function () {
            Grid.LoadData();
            $('#btnSync').click(function () {
                var index = layer.load(1);
                $.ajax({
                    url: "/Order/SyncOrders",
                    type: "post",
                    dataTyep: 'json',
                    success: function (data) {
                        if (data.success) {
                            layer.close(index);
                            Grid.Refresh();
                        }
                    }
                });
            });

            $('#btnAllot').click(function () {
                var rows = $("#grid-list").datagrid('getChecked');
                if (rows.length === 0) {
                    layer.open({
                        type: 4,
                        shade: 0,
                        time: 2000,
                        content: ['请选择需要分配的订单', '#btnAllot']//这里content是一个普通的String
                    });
                } else {

                    var index = layer.open({
                        type: 2,
                        title: '选择物流供应商',
                        content: "/Supplier/Index",
                        shade: 0,
                        area: ["600px", "500px"],
                        btn: ['确定', '取消'],
                        yes: function () {
                            var orderIds = [];
                            $.each(rows, function (i, v) {
                                orderIds.push(v.FOrderId);
                            });

                            var suppliers = [];
                            $.each(supplierGrid.GetChecked(), function (i, v) {
                                suppliers.push(v.FItemId);
                            });

                            $.ajax({
                                url: '/Order/Dispath',
                                dataType: 'json',
                                type: 'post',
                                data: {
                                    orders: JSON.stringify(orderIds),
                                    suppliers: JSON.stringify(suppliers)
                                },
                                success: function (data) {
                                    if (data.success) {
                                        layer.close(index);
                                        Grid.Refresh();
                                        layer.msg('物流分配成功！');
                                    } else {
                                        layer.msg(data.msg);
                                    }
                                }
                            });
                        }
                    });
                }
            });

        });

        var Grid = {
            LoadData: function () {
                var url = "/Order/ListData";
                Grid.list = $("#grid-list").datagrid({
                    url: url,
                    title: '订单物流预选',
                    fit: true,
                    fitColumns: true,
                    // pagination:true,
                    singleSelect: false,
                    rownumbers: true,
                    nowrap: false,
                    striped: true, //隔行变色
                    toolbar: '#toolbar',
                    columns: [[
                        { field: 'ck', checkbox: true, align: 'left', width: 50 },
                        { field: 'FOrderNo', title: '订单编号', width: 100, align: 'center' },
                        { field: 'FStoreName', title: '门店', width: 100, align: 'center' },
                        { field: 'FVipName', title: '会员名称', width: 100, align: 'center' },
                        { field: 'FShrName', title: '姓名', width: 100, align: 'center' },
                        { field: 'FShrTel', title: '电话', width: 100, align: 'center' },
                        { field: 'FAddress', title: '联系地址', width: 100, align: 'center' },
                        { field: 'FIndex', title: '行号', width: 50, align: 'center' },
                         { field: 'FItemName', title: '产品名称', width: 100, align: 'center' },
                         { field: 'FGgModel', title: '产品型号', width: 100, align: 'center' },
                         { field: 'FQty', title: '数量', width: 50, align: 'center' },
                         { field: 'FWeight', title: '重量', width: 50, align: 'center' },
                         { field: 'FVolume', title: '体积', width: 100, align: 'center' },
                         { field: 'FBjPrice', title: '保价金额', width: 100, align: 'center' },
                        { field: 'FDt', title: '有无电梯', width: 100, align: 'center' }
                    ]],
                    onLoadSuccess: onLoadSuccess
                });
            },
            Refresh: function () {
                Grid.list.datagrid('reload');
            }
        }

        function onLoadSuccess(data) {
            var _orderid;
            var _index = -1;
            var _rowspan = 0;
            for (var i = 0; i < data.rows.length; i++) {
                if (_orderid != data.rows[i].FOrderId) {
                    //合并；
                    if (_rowspan > 1) {
                        mergeCell(_index, _rowspan);
                    }
                    _orderid = data.rows[i].FOrderId;
                    _index = i;
                    _rowspan = 1;
                } else {
                    _rowspan = _rowspan + 1;
                }
            }

            if (_rowspan > 1) {
                mergeCell(_index, _rowspan);
            }
        }

        function mergeCell(_index, _rowspan) {
            $(Grid.list).datagrid('mergeCells', {
                index: _index,
                field: 'ck',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FOrderNo',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FStoreName',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FVipName',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FShrName',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FShrTel',
                rowspan: _rowspan
            }).datagrid('mergeCells', {
                index: _index,
                field: 'FAddress',
                rowspan: _rowspan
            });
        }
    </script>
}